import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
import router from '@ohos.router';

async function enterImmersion(windowClass: window.Window) {
  // 获取状态栏和导航栏的高度
  windowClass.on("avoidAreaChange", ({ type, area }) => {
    if (type == window.AvoidAreaType.TYPE_SYSTEM) {
      // 将状态栏和导航栏的高度保存在AppStorage中
      AppStorage.SetOrCreate<number>("topHeight", area.topRect.height);
      AppStorage.SetOrCreate<number>("bottomHeight", area.bottomRect.height);
      console.info('area', JSON.stringify(area))
    }
  })
  // 设置窗口布局为沉浸式布局
  await windowClass.setWindowLayoutFullScreen(true)
  await windowClass.setWindowSystemBarEnable(["status", "navigation"])
  // 设置状态栏和导航栏的背景为透明
  await windowClass.setWindowSystemBarProperties({
    navigationBarColor: "#00000000",
    statusBarColor: "#00000000",
    navigationBarContentColor: "#FF0000",
    statusBarContentColor: "#FF0000"
  })
}

// let selectPage = "";
// let currentWindowStage = null;


export default class EntryAbility extends UIAbility {
  storage: LocalStorage = new LocalStorage({
    "widget": ''
  });

  onCreate(want, launchParam) {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
    if (want.parameters.params !== undefined) {
      let params = JSON.parse(want.parameters.params);
      console.info("onNewWant router targetPage:" + params.message);
      if (params.message == 1) {
        this.storage.link('widget').set('pages/ProductDetails')
      }
    }
  }

  onDestroy() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onNewWant(want, launchParam) {
    console.info("onNewWant want:" + JSON.stringify(want));
    if (want.parameters.params !== undefined) {
      // let params = JSON.parse(want.parameters.params);
      // console.info("onNewWant router targetPage22:" + params.message);
      // selectPage = params.message;
      router.replaceUrl({
        url: "pages/ProductDetails"
      })
    }
    // if (currentWindowStage != null) {
    //   this.onWindowStageCreate(currentWindowStage);
    // }
  }


  async onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
    let windowClass: window.Window = await windowStage.getMainWindow()
    enterImmersion(windowClass)

    // let targetPage;
    // // 根据传递的targetPage不同，选择拉起不同的页面
    // switch (selectPage) {
    //   case '1':
    //     targetPage = 'pages/ProductDetails';
    //     break;
    //   default:
    //     targetPage = 'pages/SplashPage';
    // }
    // if (currentWindowStage === null) {
    //   currentWindowStage = windowStage;
    // }
    // windowStage.loadContent(targetPage, this.storage, (err, data) => {
    //   if (err && err.code) {
    //     console.info('Failed to load the content. Cause: %{public}s', JSON.stringify(err));
    //     return;
    //   }
    // });
    windowStage.loadContent('pages/SplashPage', this.storage, (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });
  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}
